package com.zyf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyf.common.result.Result;
import com.zyf.common.util.MD5;
import com.zyf.model.hosp.HospitalSet;
import com.zyf.service.HospitalSetService;
import com.zyf.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@SuppressWarnings("all")
/**
 * @description: 医院设置 Controller层
 * @projectName:yygh_parent
 * @see:com.zyf.controller
 * @author:张忆敏
 * @createTime:2021/4/26 15:58
 * @version:1.0
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置信息Api")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }


    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id逻辑删除医院设置Api")
    @ApiImplicitParam(name = "id",value = "医院设置id",required = true)
    public Result removeHospitalSet(@PathVariable long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PostMapping("/findPageHospSet/{current}/{limit}")
    @ApiOperation(value = "分页条件查询医院设置Api")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数，默认 10"),
            @ApiImplicitParam(name = "hospitalSetQueryVo", value = "条件查询实体类")
    })
    public Result findPageHospSet(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        Page<HospitalSet> page = new Page<>(current,limit);

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);

        return Result.ok(hospitalSetPage);
    }


    @PostMapping("/saveHospitalSet")
    @ApiOperation(value = "添加医院设置Api")
    @ApiImplicitParam(name = "hospitalSet", value = "添加的医院设置实体类")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){

        //设置状态  1可用   0不可用
        hospitalSet.setStatus(1)
                .setSignKey(MD5.encrypt(System.currentTimeMillis()+""+new Random().nextInt(1000))); //设置密钥

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }


    @GetMapping("/getHospSet/{id}")
    @ApiOperation(value = "根据id查询医院设置Api")
    @ApiImplicitParam(name = "id",value = "医院设置id",required = true)
    public Result getHospSetById(@PathVariable long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    @PostMapping("/updateHospitalSet")
    @ApiOperation(value = "根据id修改医院设置Api")
    @ApiImplicitParam(name = "hospitalSet",value = "修改的医院设置实体类")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @DeleteMapping("/batchRemove")
    @ApiOperation(value = "批量删除医院设置Api")
    @ApiImplicitParam(name = "ids",value = "医院设置id集合",dataType = "List")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> ids){
        boolean b = hospitalSetService.removeByIds(ids);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PutMapping("/lockHospitalSet/{id}/{status}")
    @ApiOperation(value = "医院设置锁定和解锁Api")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医院设置id"),
            @ApiImplicitParam(name = "status", value = "医院设置的状态")
    })
    public Result lockHospitalSet(@PathVariable long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PutMapping("/sendKey/{id}")
    @ApiOperation(value = "发送验证钥匙的API")
    public Result sendKey(@PathVariable long id){
        HospitalSet byId = hospitalSetService.getById(id);
        if (byId!=null){
            String signKey = byId.getSignKey();
            String hoscode = byId.getHoscode();
            //TODO 发送短信
            return Result.ok();
        }
        return Result.fail("医院设置不存在");
    }
}
